<?php
namespace app\common\model;

use think\Session;

class CategoryModel extends BaseModel
{
    protected $name = 'category'; //定义栏目表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改

    /*读取器*/
    public function getCatShowTextAttr($value, $data)
    {
        $status = [0 => '不显示', 1 => '显示',];
        return $status[$data['cat_show']];
    }

    public function getCatTypeTextAttr($value, $data)
    {
        $status = [1 => '频道页', 2 => '列表页', 3 => '外链'];
        return $status[$data['cat_type']];
    }

    /**
     * 获取树型列表
     */
    private function getCatList($mid)
    {
        $category = $this->where('mid',$mid)->order('cat_order')->select();
        $category = obj2array($category, ['cat_type_text', 'cat_show_text']);
        $category = \org\util\TreeUtil::tree($category, 'cat_name', 'cid', 'pid');
        return $category;
    }

    /**
     * 更新缓存
     */
    public function updateCache($mid)
    {
        try{
            $category  = $this->getCatList($mid);
            $moudle = cache('moudle');
            $data      = [];
            foreach ($category as $v) {
                $i = [2,4,6];
                foreach ($i as $key=>$val){
                    $v["extend".$val."_text"] = format_images_array($v["extend".$val]);
                }
                $ii = [7,8,9];
                foreach ($ii as $key=>$val){
                    $v["extend".$val."_text"] = html_out($v["extend".$val]);
                }
                $isSon            = \org\util\TreeUtil::hasChild($category, $v['cid'], 'pid');
                $v['is_son']      = $isSon;
                $v['moudle_name'] = $moudle[$v['mid']]['moudle_name'];
                $data[$v['cid']]  = $v;
            }
            cache('category_'.$mid, $data);
            return true;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

    /**
     * 获取模型选择栏目
     */
    public function getSelectCat($mid,$cid){
        $groupId = Session::get('group_id');
        $cmsCategoryCache = $this->getSubCategory($groupId,$mid);
        $category=[];
        //栏目必须处理到各自模型
        foreach ($cmsCategoryCache as $cat) {
            //排除外部链接
            if ($cat['cat_type'] == 3) {
                continue;
            }
            //非本模型栏目不显示
            if ($mid != $cat['mid']) {
                continue;
            }
            if ($cat['cat_type'] == 1) { //频道页不允许添加内容
                $cat['disabled'] = ' disabled';
            } else {
                $cat['disabled'] = '';
            }
            //当前栏目默认选中
            if ($cid == $cat['cid']) {
                $cat['selected'] = ' selected';
            } else {
                $cat['selected'] = '';
            }
            $category[]=$cat;
        }
        return $category;
    }

    /**
     * 获取投稿栏目
     */
    public function getSubCategory($groupId, $mid){
        try{
            $category = cache('category_'.$mid);
            $authGroupModel = new AuthGroupModel();
            $subCids = $authGroupModel
                ->where('group_id', $groupId)
                ->where('status', 1)
                ->column('sub_cids');
            foreach($subCids as $k=>$v){
                if(empty($v)) unset($subCids[$k]);
            }
            if (empty($subCids)) {
                return [];
            }
            $subCids = array_unique(explode(',',implode(',',$subCids))); //去重
            if (!in_array('all',$subCids)) {
                foreach ($category as $k => $v) {
                    if (in_array($k, explode(',', $subCids))) {
                        $subcategory[$k] = $v;
                    }
                }
            }else{
                $subcategory = $category;
            }
            return $subcategory;
        } catch (\Exception $e){
            exception($e->getMessage(),40001);
        }
    }

}
